package com.android.showRoomApp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * 登录页
 */
public class SettingActivity extends AppCompatActivity {

    private EditText editTextNo;
    private EditText editTextPassword;
    private Button buttonLogin;

    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        sharedPreferences = getSharedPreferences(getString(R.string.app_shareFile),MODE_PRIVATE);

        editTextNo = findViewById(R.id.loginAct_editText_no);
        editTextPassword = findViewById(R.id.loginAct_editText_password);
        buttonLogin = findViewById(R.id.loginAct_button_login);

        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (editTextNo.getText().toString().length() <=0 || editTextPassword.getText().toString().length() <= 0) {
                    // 请输入后再点击
                    return;
                }
                checkExhibitionHall(editTextNo.getText().toString(),editTextPassword.getText().toString());
            }
        });


        // 如果以前保存过，直接显示出来
        String no = sharedPreferences.getString("no","");
        String password = sharedPreferences.getString("password","");
        if (!no.isEmpty() && !password.isEmpty()){
            editTextNo.setText(no);
            editTextPassword.setText(password);
        }
    }


    private void checkExhibitionHall(String no,String password){
        RequestBody body = new FormBody.Builder().add("no",no).add("password",password).build();
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(getString(R.string.app_ip)+"/rest/localServer/checkExhibitionHall")
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                //解析响应
                ResponseBody responseBody = response.body();
                String responseString = responseBody.string();
                JsonObject responseObject = new Gson().fromJson(responseString, JsonObject.class);
                if (responseObject.get("success").getAsBoolean()){
                    //配置成功

                    //1 保存用户信息
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("no",editTextNo.getText().toString());
                    editor.putString("password",editTextPassword.getText().toString());
                    if (editor.commit()){
                        //保存成功
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(SettingActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else {
                        //保存失败
                        return;
                    }

                    //2 跳转页面
                    Intent intent = new Intent(SettingActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    //配置失败
                    final String errorMsg = responseObject.get("msg").getAsString();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(SettingActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                Log.i("zjq",responseObject.toString());
            }

            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.i("zjq",e.toString());
            }
        });
    }




}
